const http = require('../../utils/http')
const api = require('../../utils/api')
// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailId: '',
    roomName: '',
    showCancel: false,
    cancelStatus: '2',
    showWechat: false,
    detail: {

    },
  },

  loadBusinessData() {
    http.request({
      showLoading: true,
      url: api.loadTradeDetail,
      data: {
        tradeId: this.data.detailId
      },
    }).then(res => {
      if (res.data.code == 200) {
        this.setData({
          'detail': res.data.result,
        })
        if(res.data.result.payInfos.length == 1){
          if(res.data.result.payInfos[0].payMode == 1){
            this.setData({
              showWechat: true,
            })
          }
        }
      }
    }).catch(err => {
      console.log(err)
    });
  },

  closeview : function(e){
    wx.navigateBack({
      delta: 1,
    })
  },
  preventTouchMove:function(e) {

  },
  cancelOrder(e) {
    this.setData({
      showCancel: true,
      cancelStatus: '2'
    })
  },
  hiddenCancel(e) {
    this.setData({
      showCancel: false,
    })
  },
  weChatCancel(e){
    this.setData({
      cancelStatus: '1',
    })
  },
  remainingCancel(e){
    this.setData({
      cancelStatus: '2',
    })
  },
  confirmCancel(e){
    http.request({
      showLoading: true,
      url: api.cancelTrade,
      method: "POST",
      data: {
        cancelMode: this.data.cancelStatus,
        tradeId: this.data.detailId
      },
    }).then(res => {
      if (res.data.code == 200) {
        this.hiddenCancel();
        setTimeout(function () {
          wx.showToast({
            title: '订单取消成功',
            icon: "none",
            duration: 2000
          });
        }, 500);
        wx.navigateBack({
          delta: 1,
        })
      }else{
        setTimeout(function () {
          wx.showToast({
            title: '订单取消失败，请稍后再试',
            icon: "none",
            duration: 2000
          });
        }, 500);
      }
    }).catch(err => {
      console.log(err)
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    console.log(options);
    that.setData({
      roomName: options.detailId,
      detailId: options.detailId,
    })
    that.loadBusinessData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})